package com.af.community.smart.business

import android.graphics.Bitmap
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.makeCall
import com.af.community.smart.share.BaseUrl
import kotlinx.android.synthetic.main.activity_notice_detail.*
import org.jetbrains.anko.browse

class NoticeDetailActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notice_detail)
        initTitle(title = "详情")

        val mTitle = intent.getStringExtra("title").orEmpty()
        val mContent = intent.getStringExtra("content").orEmpty()
        val mType = intent.getStringExtra("type").orEmpty()
        val mTime = intent.getStringExtra("time").orEmpty()

        notice_title.text = mTitle
        notice_type.text = mType
        notice_time.text = mTime

        val str = "<!doctype html><html>\n" +
                "<meta charset=\"utf-8\">" +
                "<style type=\"text/css\">" +
                "body{ padding:0; margin:0; }\n" +
                ".con{ width:95%; margin:0 auto; color:#666; padding:0.5em 0; overflow:hidden; display:block; font-size:0.92em; line-height:1.8em; }\n" +
                ".con h1,h2,h3,h4,h5,h6{ font-size:1em; }\n " +
                "img{ width:auto; max-width: 100% !important; height:auto !important; margin:0 auto; display:block; }\n" +
                "*{ max-width:100% !important; }\n" +
                "</style>\n" +
                "<body style=\"padding:0; margin:0; \">" +
                "<div class=\"con\">" +
                mContent +
                "</div>" +
                "</body>" +
                "</html>"

        notice_web.loadDataWithBaseURL(BaseUrl.baseHttp, str, "text/html", "utf-8", "")
    }

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        notice_web.apply {
            //支持javascript
            @Suppress("DEPRECATION")
            settings.javaScriptEnabled = true
            //设置可以支持缩放
            settings.setSupportZoom(true)
            //自适应屏幕
            settings.loadWithOverviewMode = true
            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            isHorizontalScrollBarEnabled = false

            //设置出现缩放工具
            settings.builtInZoomControls = true
            settings.displayZoomControls = false

            //设置是否使用缓存
            settings.setAppCacheEnabled(true)
            settings.domStorageEnabled = true

            webChromeClient = WebChromeClient()
            webViewClient = object : WebViewClient() {

                /* 这个事件，将在用户点击链接时触发。
                 * 通过判断url，可确定如何操作，
                 * 如果返回true，表示我们已经处理了这个request，
                 * 如果返回false，表示没有处理，那么浏览器将会根据url获取网页
                 */
                @Suppress("DEPRECATION", "OverridingDeprecatedMember")
                override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                    return when {
                        "tel:" in url -> makeCall(url.replace("tel:", ""))
                        !url.startsWith("https://")
                                && !url.startsWith("http://") -> {
                            if (url.startsWith("weixin://wap/pay?")) browse(url)
                            true
                        }
                        url.endsWith(".apk") -> browse(url)
                        else -> {
                            view.loadUrl(url)
                            true
                        }
                    }
                }

                /*
                 * 在开始加载网页时会回调
                 */
                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                    super.onPageStarted(view, url, favicon)
                    showLoading()
                }

                /*
                 * 在结束加载网页时会回调
                 */
                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    dismissLoading()
                }

            }
        }
    }

}
